<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是C语言——数组与字符串相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-05-C语言-数组与字符串">
<meta property="og:url" content="https://sumumm.github.io/post/582937ae.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是C语言——数组与字符串相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128110245590.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128111357350.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128112243966.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128124741125.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128125248664.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128161750817.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128161709639.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128185210049.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128185825245.png">
<meta property="article:published_time" content="2022-01-14T08:29:55.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.984Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV01-C语言基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128110245590.png">


<link rel="canonical" href="https://sumumm.github.io/post/582937ae.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/582937ae.html","path":"post/582937ae.html","title":"LV01-05-C语言-数组与字符串"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-05-C语言-数组与字符串 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E6%95%B0%E7%BB%84"><span class="nav-text">一、数组</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">1.一维数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E5%AE%9A%E4%B9%89"><span class="nav-text">1.1一维数组定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E8%AE%BF%E9%97%AE"><span class="nav-text">1.2一维数组元素访问</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">1.3一维数组元素初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E7%9A%84%E5%AD%98%E5%82%A8%E7%A9%BA%E9%97%B4"><span class="nav-text">1.4一维数组的存储空间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E7%9A%84%E6%95%B0%E7%BB%84%E5%90%8D"><span class="nav-text">1.5一维数组的数组名</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">2.二维数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%A3%B0%E6%98%8E"><span class="nav-text">2.1二维数组声明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E8%AE%BF%E9%97%AE"><span class="nav-text">2.2二维数组访问</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">2.3二维数组元素初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E7%9A%84%E5%AD%98%E5%82%A8%E7%A9%BA%E9%97%B4"><span class="nav-text">2.4二维数组的存储空间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E7%9A%84%E6%95%B0%E7%BB%84%E5%90%8D"><span class="nav-text">2.5二维数组的数组名</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E4%B8%AA%E6%95%B0"><span class="nav-text">3.数组元素个数</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8E%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84"><span class="nav-text">二、字符串与字符数组</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">1.字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AE%9A%E4%B9%89"><span class="nav-text">1.1字符串定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%95%BF%E5%BA%A6"><span class="nav-text">1.2字符串长度</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84"><span class="nav-text">2.字符数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84%E5%AE%9A%E4%B9%89"><span class="nav-text">2.1字符数组定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">2.2字符数组初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84%E8%AE%BF%E9%97%AE"><span class="nav-text">2.3字符数组访问</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1%E9%80%90%E4%B8%AA%E5%AD%97%E7%AC%A6%E8%AE%BF%E9%97%AE%E8%BE%93%E5%87%BA"><span class="nav-text">2.3.1逐个字符访问输出</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-2%E4%BB%A5%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%BD%A2%E5%BC%8F%E8%AE%BF%E9%97%AE%E8%BE%93%E5%87%BA"><span class="nav-text">2.3.2以字符串的形式访问输出</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0"><span class="nav-text">3字符串处理函数</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/582937ae.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-05-C语言-数组与字符串 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-05-C语言-数组与字符串
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-01-14 16:29:55" itemprop="dateCreated datePublished" datetime="2022-01-14T16:29:55+08:00">2022-01-14</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV01-C语言基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>21 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是C语言——数组与字符串相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、数组"><a href="#一、数组" class="headerlink" title="一、数组"></a><font size=3>一、数组</font></h1><p>数组是<strong>构造数据类型</strong>之一，是具有一定顺序关系的若干个<strong>变量</strong>的<strong>集合</strong>，组成数组的各个变量称为数组的<strong>元素</strong>，数组中各元素的<strong>数据类型要求相同</strong>，用<strong>数组名</strong>和<strong>下标</strong>确定。数组可以是一维的，也可以是多维的。</p>
<h2 id="1-一维数组"><a href="#1-一维数组" class="headerlink" title="1.一维数组"></a><font size=3>1.一维数组</font></h2><h3 id="1-1一维数组定义"><a href="#1-1一维数组定义" class="headerlink" title="1.1一维数组定义"></a><font size=3>1.1一维数组定义</font></h3><ul>
<li>概念</li>
</ul>
<p>一维数组是指<strong>只有一个下标</strong>的数组。它在计算机的内存中是<strong>连续存储</strong>的。</p>
<ul>
<li>一维数组声明</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">storage_type data_type array_name[ array_size ];</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=100px>storage_type</td>
        <td align="left">存储类型（可以说明也可以不说明）</td>
    </tr>
    <tr>
        <td align="center" width=100px>data_type</td>
        <td align="left">任意有效的 C 数据类型（必须说明）</td>
    </tr>
    <tr>
        <td align="center" width=100px>array_name</td>
        <td align="left">数组名称（必须说明）</td>
    </tr>
    <tr>
        <td align="center" width=100px>array_size</td>
        <td align="left">数组长度（有些情况可以省略）</td>
    </tr>
</table>



<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">float</span> m[<span class="number">12</span>];  <span class="comment">/* 定义一个长度为 12 的浮点型数组 */</span></span><br><span class="line"><span class="type">char</span> ch[<span class="number">9</span>];   <span class="comment">/* 定义一个长度为 9 的字符型数组 */</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">1</span>];     <span class="comment">/* 定义一个长度为 9 的整型数组 */</span></span><br><span class="line"><span class="type">int</span> b[] = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;; <span class="comment">/* 定义了一个长度为3，且进行了初始化的整形数组 */</span></span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）数组必须<strong>先声明</strong>才能进行使用和赋值。</p>
<p>（2） C语言 对数组<strong>不作越界检查</strong>，使用时要注意。</p>
<p>（3）用变量定义数组维数时，该变量必须提前告知。例如 int i &#x3D; 6;int a[i]; </p>
<p>（4） sizeof(array_name) 可以获取整个一维数组所占据的字节数（<strong>等于元素个数x元素类型所占字节数</strong>）。</p>
<h3 id="1-2一维数组元素访问"><a href="#1-2一维数组元素访问" class="headerlink" title="1.2一维数组元素访问"></a><font size=3>1.2一维数组元素访问</font></h3><ul>
<li>访问格式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">array_name[index]  <span class="comment">/* 数组名[元素索引（也可以说是下标）] 索引从0开始 */</span></span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">10</span>];</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(“%d”, a);          <span class="comment">/* （错误） 这种访问方式是错误的 */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;<span class="number">10</span>;j++)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(“%d\t”, a[j]); <span class="comment">/* (正确) 这种情况才是正确的访问方式 */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）数组必须定义之后才能够访问。</p>
<p>（2）数组的元素只能逐个访问，不能一次性访问整个数组。</p>
<p>（3）下标可以是整型表达式或者常量。</p>
<ul>
<li>遍历一维数组</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">10</span>];</span><br><span class="line"><span class="type">int</span> i, n;</span><br><span class="line"></span><br><span class="line">n = <span class="keyword">sizeof</span>(a) / <span class="keyword">sizeof</span>(<span class="type">int</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 通过for循环来逐个访问 */</span></span><br><span class="line"><span class="keyword">for</span>(j = <span class="number">0</span>; j &lt; n; j++)</span><br><span class="line">    <span class="built_in">printf</span>(“%d\t”, a[j]); <span class="comment">/* 逐个打印数组元素 */</span></span><br></pre></td></tr></table></figure>

<h3 id="1-3一维数组元素初始化"><a href="#1-3一维数组元素初始化" class="headerlink" title="1.3一维数组元素初始化"></a><font size=3>1.3一维数组元素初始化</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">6</span>] = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;; <span class="comment">/* 在声明数组时直接进行初始化 */</span></span><br><span class="line"><span class="type">int</span> b[] = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;;           <span class="comment">/* 定义了一个长度为3，且进行了初始化的整形数组 */</span></span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）数组不初始化，其元素值为随机数</p>
<p>（2）对 static 数组元素不赋初值，系统会自动赋以 0 值</p>
<p>（3）只给部分数组元素赋初值，剩余元素自动赋值为 0 。</p>
<p>【说明】</p>
<table>
    <tr>
        <td align="center">数组声明形式</td>
        <td align="center">等价写法</td>
    </tr>
    <tr>
        <td align="left">static int a[3];</td>
        <td align="left">static int a[3]; a[0] = 0;  a[1] = 0; a[2] = 0;</td>
    </tr>
    <tr>
        <td align="left">int a[3] = {1, 2};</td>
        <td align="left">int a[3]; a[0] = 1;  a[1] = 2; a[2] = 0;</td>
    </tr>
    <tr>
        <td align="left">int a[] = {1, 2, 3}</td>
        <td align="left">int a[3]; a[0] = 1;  a[1] = 2; a[2] = 3;<br> /* 编译系统根据初值个数确定数组维数 */</td>
    </tr>
</table>



<h3 id="1-4一维数组的存储空间"><a href="#1-4一维数组的存储空间" class="headerlink" title="1.4一维数组的存储空间"></a><font size=3>1.4一维数组的存储空间</font></h3><p>数组是一个整体，它的<strong>内存是连续</strong>的，相邻元素地址相差  data_type 所占字节长度。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a[<span class="number">3</span>] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;&amp;a[%d] = %p, a[%d] = %d\n&quot;</span>,i, &amp;a[i], i,a[i]);</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128110245590.png" alt="image-20220128110245590" style="zoom:67%;" />

<p> a[3] 在内存中的结构如下图所示， a[3] 数组为 int 类型，在 64 位平台下占 4 个字节，所以相邻元素之间地址相差 4 。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128111357350.png" alt="image-20220128111357350" style="zoom: 67%;" />

<h3 id="1-5一维数组的数组名"><a href="#1-5一维数组的数组名" class="headerlink" title="1.5一维数组的数组名"></a><font size=3>1.5一维数组的数组名</font></h3><p>一维数组的数组名代表了该数组的起始地址，可以通过该数名推出各个元素的地址，并通过指针（后边说，先提一下）进行访问。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a[<span class="number">3</span>] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %p\n&quot;</span>,a);</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;a + %d = %p, &amp;a[%d] = %p, a[%d] = %d\n&quot;</span>,i, a + i, i, &amp;a[i], i,a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128112243966.png" alt="image-20220128112243966" style="zoom:67%;" />

<p>【注意】数组名<strong>不可以进行自加自减</strong>运算。</p>
<h2 id="2-二维数组"><a href="#2-二维数组" class="headerlink" title="2.二维数组"></a><font size=3>2.二维数组</font></h2><h3 id="2-1二维数组声明"><a href="#2-1二维数组声明" class="headerlink" title="2.1二维数组声明"></a><font size=3>2.1二维数组声明</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">数据类型 数组名[ 常量表达式<span class="number">1</span> ][ 常量表达式<span class="number">2</span> ];</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; <span class="comment">/* 定义了一个2行3列的二维数组 */</span></span><br></pre></td></tr></table></figure>

<p>表示定义了一个 2行3列的二维数组 ，元素共有 2x3&#x3D;6 个（ 元素个数 &#x3D; 行数 x 列数 ）。</p>
<p>【注意】</p>
<p>（1）声明时<strong>列数不能省略</strong>，**行数可以省略(定义时进行初始化，编译器可以判别出行数时)**。</p>
<p>（2） sizeof(数组名) 可以获取整个二维数组所占据的字节数（<strong>等于元素个数x元素类型所占字节数</strong>）。</p>
<p>（3）2） sizeof(行数组名) 可以获取二维数组某行所占据的字节数（<strong>等于该行元素个数x元素类型所占字节数</strong>）。</p>
<h3 id="2-2二维数组访问"><a href="#2-2二维数组访问" class="headerlink" title="2.2二维数组访问"></a><font size=3>2.2二维数组访问</font></h3><ul>
<li>访问格式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">array_name[row_index][col_index]  <span class="comment">/* 数组名[行索引][列索引] 行索引和列索引都是从0开始*/</span></span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=100px>storage_type</td>
        <td align="left">存储类型（可以说明也可以不说明）</td>
    </tr>
    <tr>
        <td align="center" width=100px>data_type</td>
        <td align="left">任意有效的 C 数据类型（必须说明）</td>
    </tr>
    <tr>
        <td align="center" width=100px>p_name</td>
        <td align="left">指针变量名</td>
    </tr>
</table>


<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>];</span><br><span class="line"><span class="type">int</span> i, j;</span><br><span class="line"><span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">2</span>;i++)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;<span class="number">3</span>;j++)</span><br><span class="line">    &#123;</span><br><span class="line">       <span class="built_in">printf</span>(“%d\t”, a[i][j]); <span class="comment">/* (正确) 这种情况才是正确的访问方式 */</span> </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>遍历二维数组</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>];</span><br><span class="line"><span class="type">int</span> i, j;</span><br><span class="line"><span class="comment">/* 通过for循环进行逐个元素的访问 */</span></span><br><span class="line"><span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">2</span>;i++)       <span class="comment">/* 行循环 */</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;<span class="number">3</span>;j++)  <span class="comment">/* 列循环 */</span></span><br><span class="line">    &#123;</span><br><span class="line">       <span class="built_in">printf</span>(“%d\t”, a[i][j]); </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-3二维数组元素初始化"><a href="#2-3二维数组元素初始化" class="headerlink" title="2.3二维数组元素初始化"></a><font size=3>2.3二维数组元素初始化</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>] = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;&#125;; <span class="comment">/* 分行赋值 */</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;;     <span class="comment">/* 按数组排列顺序进行赋值 */</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>] = &#123;&#123;<span class="number">1</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>&#125;;           <span class="comment">/* 对部分元素赋初值 */</span></span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）与一维数组相同，数组不初始化，其元素值为随机；对 static 数组元素不赋初值，系统会自动赋以 0 值；只给部分数组元素赋初值，剩余元素自动赋值为 0 。</p>
<p>（2）当采用分行赋值的方法声明二维数组时，二维数组的行数可以省略，行数会由编译器自己进行计算。</p>
<p>【说明】</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 声明形式 1 (两种写法等价) */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>];</span><br><span class="line"><span class="type">static</span> <span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; a[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">0</span>; a[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">0</span>; a[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">0</span>; a[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">0</span>;</span><br><span class="line">    </span><br><span class="line"><span class="comment">/* 声明形式 2 (两种写法等价) */</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>] = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;&#125;;</span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; a[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">1</span>; a[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">2</span>; a[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">3</span>; a[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">4</span>; a[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">5</span>; a[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">6</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 声明形式 3 (两种写法等价) */</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>] = &#123;&#123;<span class="number">1</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>&#125;&#125;;</span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; a[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">1</span>; a[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">0</span>; a[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">4</span>; a[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">5</span>; a[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 声明形式 4 (两种写法等价) */</span></span><br><span class="line"><span class="type">int</span> a[ ][<span class="number">3</span>] = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;&#125;;</span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; a[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">1</span>; a[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">2</span>; a[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">3</span>; a[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">4</span>; a[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">5</span>; a[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">6</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 声明形式 5 (两种写法等价) */</span></span><br><span class="line"><span class="type">int</span> a[ ][<span class="number">3</span>] = &#123;&#123;&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>&#125;&#125;;</span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; a[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">0</span>; a[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">4</span>; a[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">5</span>; a[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 声明形式 6 (两种写法等价) */</span></span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;;</span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; a[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">0</span>; a[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">4</span>; a[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">5</span>; a[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">0</span>;<span class="comment">/* 编译器自己计算赋值，不过会有警告*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 声明形式 7 (两种写法等价) */</span></span><br><span class="line"><span class="type">int</span> a[ ][<span class="number">3</span>] = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;;</span><br><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; a[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">0</span>; a[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">0</span>; a[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">4</span>; a[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">5</span>; a[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">0</span>;<span class="comment">/* 编译器自己计算赋值，不过会有警告*/</span></span><br></pre></td></tr></table></figure>


<h3 id="2-4二维数组的存储空间"><a href="#2-4二维数组的存储空间" class="headerlink" title="2.4二维数组的存储空间"></a><font size=3>2.4二维数组的存储空间</font></h3><p>二维数组在内存中是一维的，存储时行序优先。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span> </span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a[][<span class="number">3</span>] = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;&#125;;</span><br><span class="line">	<span class="type">int</span> i, j;</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">2</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;&amp;a[%d][%d] = %p, a[%d][%d] = %d\n&quot;</span>,i, j, &amp;a[i][j],i, j, a[i][j]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128124741125.png" alt="image-20220128124741125" style="zoom:67%;" />

<p> a[2][3] 在内存中的结构如下图所示， a[2][3] 数组为 int 类型，在 64 位平台下占 4 个字节，所以相邻元素之间地址相差 4 。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128125248664.png" alt="image-20220128125248664" style="zoom:67%;" />

<h3 id="2-5二维数组的数组名"><a href="#2-5二维数组的数组名" class="headerlink" title="2.5二维数组的数组名"></a><font size=3>2.5二维数组的数组名</font></h3><p>与一维数组一样，二维数组的数组名也代表了该二维数组的起始地址。二维数组的另一种理解方式：二维数组可以看做是由多个元素组成的一维数组，而每个元素又是一个数组，从而合起来构成了二维数组。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span> </span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">	<span class="type">int</span> a[][<span class="number">3</span>] = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;&#125;;</span><br><span class="line">	<span class="type">int</span> i, j;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %p\n&quot;</span>, a);</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">2</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;a + %d = %p, a[%d] = %p\n&quot;</span>,i,a +i, i, a[i]);</span><br><span class="line">		<span class="keyword">for</span>(j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;&amp;a[%d][%d] = %p, a[%d][%d] = %d\n&quot;</span>,i, j, &amp;a[i][j],i, j, a[i][j]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128161750817.png" alt="image-20220128161750817" style="zoom:67%;" />

<p>经过打印地址发现：</p>
<p>（1） a 为整个二维数组的数组名，代表了整个二维数组的起始地址。</p>
<p>（2） a[0], a[1] 也分别代表了第一行和第二行起始数据的地址。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128161709639.png" alt="image-20220128161709639" style="zoom:50%;" />

<p>经过分析和验证，可以得到：一个二维数组，按行可以分为多个一维数组，以 int a[2][3] 为例，该 2行3列 的二维数组就可以理解为 2个元素 组成，每个元素都是一个一维数组组成，每个一维数组的数组名就是二维数组名加上第一个下标，即 a[0], a[1] 。</p>
<table>
    <tr>
        <td align="center" colspan="3">行名(代表了地址)</td>
        <td align="center" colspan="3">每行元素</td>
    </tr>
    <tr>
        <td align="center">a</td>
        <td align="center">a[0]</td>
        <td align="center">&a[0][0]</td>
        <td align="center">a[0][0]</td>
        <td align="center">a[0][1]</td>
        <td align="center">a[0][2]</td>
    </tr>
    <tr>
        <td align="center">a + 1</td>
        <td align="center">a[1]</td>
        <td align="center">&a[1][0]</td>
        <td align="center">a[1][0]</td>
        <td align="center">a[1][1]</td>
        <td align="center">a[1][2]</td>
    </tr>
</table>

<p>【注意】数组名<strong>不可以进行自加自减</strong>运算，自然这里的行名就相当于每一行元素的数组名，也不可以进行自加自减运算。</p>
<h2 id="3-数组元素个数"><a href="#3-数组元素个数" class="headerlink" title="3.数组元素个数"></a><font size=3>3.数组元素个数</font></h2><p>我们有的时候需要程序去自动计算数组的长度，有下边两种方式：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">len = <span class="keyword">sizeof</span>(数组名)/<span class="keyword">sizeof</span>(数组类型);</span><br><span class="line">len = <span class="keyword">sizeof</span>(数组名)/<span class="keyword">sizeof</span>(数组名[<span class="number">0</span>]);</span><br></pre></td></tr></table></figure>

<p>已知数组元素类型的时候，我们可以使用第一种方式，当未知数组元素类型的时候，我们可以使用后边这种方式，直接使用数组中首个元素作为每个元素大小计算的标准。</p>
<h1 id="二、字符串与字符数组"><a href="#二、字符串与字符数组" class="headerlink" title="二、字符串与字符数组"></a><font size=3>二、字符串与字符数组</font></h1><h2 id="1-字符串"><a href="#1-字符串" class="headerlink" title="1.字符串"></a><font size=3>1.字符串</font></h2><h3 id="1-1字符串定义"><a href="#1-1字符串定义" class="headerlink" title="1.1字符串定义"></a><font size=3>1.1字符串定义</font></h3><p>字符串是一系列<strong>连续的字符的组合</strong>，要想在内存中定位一个字符串，除了要知道它的开头，还要知道它的结尾。找到字符串的开头很容易，知道它的名字（字符数组名或者字符串名）就可以，但是结尾怎么办呢？在 C语言 中，字符串总是以 ‘\0’ 作为结尾，所以 ‘\0’ 也被称为<strong>字符串结束标志</strong>，或者字符串结束符。由 “ “ 包围的字符串会自动在末尾添加 ‘\0’ 。例如， “abc123” 从表面看起来只包含了  6  个字符，其实不然， C语言 会在最后隐式地添加一个 ‘\0’ 。</p>
<p> ‘\0’ 是  ASCII 码 表中的第  0  个字符，英文称为  NULL ，中文称为“空字符”。该字符既不能显示，也没有控制功能，输出该字符不会有任何效果，它在 C语言 中<strong>唯一</strong>的作用就是作为字符串结束标志。</p>
<h3 id="1-2字符串长度"><a href="#1-2字符串长度" class="headerlink" title="1.2字符串长度"></a><font size=3>1.2字符串长度</font></h3><p>所谓字符串长度，就是字符串包含了多少个字符（**不包括最后的结束符 ‘\0’ **）。例如： abc123 字符串在内存中占据的空间是 6 + 1 &#x3D; 7 个字符的空间，但是这个字符串的长度为 6 。</p>
<h2 id="2-字符数组"><a href="#2-字符数组" class="headerlink" title="2.字符数组"></a><font size=3>2.字符数组</font></h2><h3 id="2-1字符数组定义"><a href="#2-1字符数组定义" class="headerlink" title="2.1字符数组定义"></a><font size=3>2.1字符数组定义</font></h3><p>元素的数据类型为字符类型的数组称为<strong>字符数组</strong>，字符数组实际上是一系列字符的集合，例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> a[<span class="number">10</span>];     <span class="comment">/* 定义一个长度为10的一维字符数组 */</span></span><br><span class="line"><span class="type">char</span> b[<span class="number">2</span>][<span class="number">10</span>];  <span class="comment">/* 定义一个2行3列的二维字符数组 */</span></span><br></pre></td></tr></table></figure>

<h3 id="2-2字符数组初始化"><a href="#2-2字符数组初始化" class="headerlink" title="2.2字符数组初始化"></a><font size=3>2.2字符数组初始化</font></h3><ul>
<li>一维字符数组初始化</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 一维字符数组初始化 */</span></span><br><span class="line"><span class="type">char</span> a[<span class="number">7</span>] = &#123;<span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;o&#x27;</span>&#125;; <span class="comment">/* 逐个字符赋值 */</span></span><br><span class="line"><span class="type">char</span> a[] = &#123;<span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;o&#x27;</span>&#125;;  <span class="comment">/* 省略长度，逐个字符赋值 */</span></span><br><span class="line"><span class="type">char</span> a[] = &#123;<span class="string">&quot;world!&quot;</span>&#125;;                 <span class="comment">/* 用字符串常量赋值 */</span></span><br><span class="line"><span class="type">char</span> a[] = <span class="string">&quot;world!&quot;</span>;                   <span class="comment">/* 用字符串常量赋值 */</span></span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）给字符数组赋值时，通常将字符串一次性地赋值（可以指明数组长度，也可以不指明），而不是一个字符一个字符地赋值。</p>
<p>（2）字符数组<strong>只有在定义时才能将整个字符串一次性地赋值</strong>给它，一旦定义完了，就只能一个字符一个字符地赋值了。</p>
<p>（3）字符数组在未赋初值时，内容也是随机的。</p>
<p>（4）当使用字符串给字符数组赋值，且未给出长度时，创建的字符数组真实长度为<strong>字符串长度 + 1</strong>，原因就在于用字符串进行赋值时，字符串结束处自带一个<strong>字符串结束标志： \0 <strong>。所以这也就意味着，我们若是要指定字符数组的大小，一定要留够</strong>字符串长度 + 1</strong>的长度，这样字符串才更加完整，也会比较严谨。</p>
<p>【说明】</p>
<table>
    <tr>
        <td align="center">字符数组声明形式</td>
        <td align="center">等价写法</td>
    </tr>
    <tr>
        <td align="center">char a[3] = {'h', 'e', 'l'};</td>
        <td align="center">char a[3]; a[0] = 'h'; a[1] = 'h'; a[2] = 'l';</td>
    </tr>
    <tr>
        <td align="center">char a[3] = {'h'};</td>
        <td align="center">char a[3]; a[0] = 'h'; a[1] = '\0'; a[2] = '\0';</td>
    </tr>
    <tr>
        <td align="center">char a[3] = {"hel"};</td>
        <td align="center">char a[3]; a[0] = 'h'; a[1] = 'e'; a[2] = 'l';<br>/* 这里若是长度定义为4就会更加合理 */</td>
    </tr>
    <tr>
        <td align="center">char a[] = {"hel"};</td>
        <td align="center">char a[4]; a[0] = 'h'; a[1] = 'e'; a[2] = 'l';a[3] = '\0';</td>
    </tr>
</table>


<ul>
<li>二维字符数组初始化</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> fruit[][<span class="number">7</span>]=&#123;<span class="string">&quot;Apple&quot;</span>, <span class="string">&quot;Orange&quot;</span>&#125;;</span><br></pre></td></tr></table></figure>

<p>效果如下表</p>
<table>
    <tr>
        <td align="center">行名</td>
        <td align="center" colspan="7">每行元素</td>
    </tr>
    <tr>
        <td align="center">fruit[0]</td>
        <td align="center">'A'</td>
        <td align="center">'p'</td>
        <td align="center">'p'</td>
        <td align="center">'l'</td>
        <td align="center">'e'</td>
        <td align="center">'\0'</td>
        <td align="center">'\0'</td>    
    </tr>
    <tr>
        <td align="center">fruit[1]</td>
        <td align="center">'O'</td>
        <td align="center">'r'</td>
        <td align="center">'a'</td>
        <td align="center">'r'</td>
        <td align="center">'g'</td>
        <td align="center">'e'</td>
        <td align="center">'\0'</td>    
    </tr>
</table>


<h3 id="2-3字符数组访问"><a href="#2-3字符数组访问" class="headerlink" title="2.3字符数组访问"></a><font size=3>2.3字符数组访问</font></h3><p>与一般的数组一样，访问元素时只能一个逐个字符访问，但是若是只输出的话要注意赋值方式的不同，输出时也会不一样。</p>
<h4 id="2-3-1逐个字符访问输出"><a href="#2-3-1逐个字符访问输出" class="headerlink" title="2.3.1逐个字符访问输出"></a><font size=3>2.3.1逐个字符访问输出</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span> </span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">	<span class="type">char</span> a[<span class="number">6</span>] = &#123; <span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;\0&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>,<span class="string">&#x27;o&#x27;</span>&#125;; <span class="comment">/* 逐个赋值，中间带有 &#x27;\0&#x27; */</span></span><br><span class="line">	<span class="type">char</span> b[] = <span class="string">&quot;h\0ello&quot;</span>;                        <span class="comment">/* 字符串常量赋值,中间带有 &#x27;\0&#x27; */</span></span><br><span class="line">	<span class="type">char</span> c[] = <span class="string">&quot;hello&quot;</span>;                          <span class="comment">/* 字符串常量赋值,中间没带有 &#x27;\0&#x27; */</span></span><br><span class="line">	<span class="type">int</span> i, n;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;---------Character by character---------\n&quot;</span>);</span><br><span class="line">	n = <span class="keyword">sizeof</span>(a)/<span class="keyword">sizeof</span>(<span class="type">char</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\nlength(a)=%d\t&quot;</span>,n);</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>,a[i]);</span><br><span class="line"></span><br><span class="line">	n = <span class="keyword">sizeof</span>(b)/<span class="keyword">sizeof</span>(<span class="type">char</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\nlength(b)=%d\t&quot;</span>,n);</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>,a[i]);</span><br><span class="line"></span><br><span class="line">	n = <span class="keyword">sizeof</span>(c)/<span class="keyword">sizeof</span>(<span class="type">char</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\nlength(c)=%d\t&quot;</span>,n);</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>,a[i]);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n------------------end------------------\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128185210049.png" alt="image-20220128185210049" style="zoom:67%;" />

<p>注意数组 b[] 打印是有问题的。</p>
<h4 id="2-3-2以字符串的形式访问输出"><a href="#2-3-2以字符串的形式访问输出" class="headerlink" title="2.3.2以字符串的形式访问输出"></a><font size=3>2.3.2以字符串的形式访问输出</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span> </span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">	<span class="type">char</span> a[<span class="number">6</span>] = &#123; <span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;\0&#x27;</span>, <span class="string">&#x27;l&#x27;</span>,<span class="string">&#x27;o&#x27;</span>&#125;; <span class="comment">/* 逐个赋值，中间带有 &#x27;\0&#x27; */</span></span><br><span class="line">	<span class="type">char</span> b[<span class="number">6</span>] = &#123; <span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>,<span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;!&#x27;</span>&#125;;  <span class="comment">/* 逐个赋值，整个字符数组不带 &#x27;\0&#x27;  */</span></span><br><span class="line">	<span class="type">char</span> c[] = <span class="string">&quot;h\0ello&quot;</span>;                        <span class="comment">/* 字符串常量赋值,中间带有 &#x27;\0&#x27; */</span></span><br><span class="line">	<span class="type">char</span> d[] = <span class="string">&quot;hello&quot;</span>;                          <span class="comment">/* 字符串常量赋值,中间没带有 &#x27;\0&#x27; */</span></span><br><span class="line">	<span class="type">int</span> n;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;---------Character by character---------\n&quot;</span>);</span><br><span class="line">	n = <span class="keyword">sizeof</span>(a)/<span class="keyword">sizeof</span>(<span class="type">char</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;length(a)=%d, a[6]=%s\n&quot;</span>, n, a);</span><br><span class="line"></span><br><span class="line">	n = <span class="keyword">sizeof</span>(b)/<span class="keyword">sizeof</span>(<span class="type">char</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;length(b)=%d, b[]=%s\n&quot;</span>, n, b);</span><br><span class="line"></span><br><span class="line">	n = <span class="keyword">sizeof</span>(c)/<span class="keyword">sizeof</span>(<span class="type">char</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;length(c)=%d,c[]=%s\n&quot;</span>, n, c);</span><br><span class="line"></span><br><span class="line">	n = <span class="keyword">sizeof</span>(d)/<span class="keyword">sizeof</span>(<span class="type">char</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;length(d)=%d,c[]=%s\n&quot;</span>, n, d);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n------------------end------------------\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-05-C%E8%AF%AD%E8%A8%80-%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/img/image-20220128185825245.png" alt="image-20220128185825245" style="zoom:67%;" />

<p>注意数组 b[] 打印是有问题的。</p>
<p>【总结】</p>
<p>（1）注意字符数组中的 \0 ，当输出时，该字符会对输出结果造成影响。</p>
<p>（2）注意在字符数组逐个赋值时，若没有字符串结束标志 \0 ，那么尽量不要使用 printf 函数的 %s 格式进行输出，就如上图中的 b[] 数组</p>
<h2 id="3字符串处理函数"><a href="#3字符串处理函数" class="headerlink" title="3字符串处理函数"></a><font size=3>3字符串处理函数</font></h2><p>在使用字符串处理函数时，要加上头文件 &lt;string.h&gt; 。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 计算字符串 str 的长度，直到空结束字符，但不包括空结束字符。 */</span></span><br><span class="line"><span class="type">size_t</span> <span class="title function_">strlen</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *str)</span> </span><br><span class="line">    </span><br><span class="line"><span class="comment">/* 把 src 所指向的字符串复制到 dest */</span></span><br><span class="line"><span class="type">char</span> *<span class="title function_">strcpy</span><span class="params">(<span class="type">char</span> *dest, <span class="type">const</span> <span class="type">char</span> *src)</span> </span><br><span class="line">    </span><br><span class="line"><span class="comment">/* 把 src 所指向的字符串追加到 dest 所指向的字符串的结尾。 */</span></span><br><span class="line"><span class="type">char</span> *<span class="title function_">strcat</span><span class="params">(<span class="type">char</span> *dest, <span class="type">const</span> <span class="type">char</span> *src)</span> </span><br><span class="line"></span><br><span class="line"><span class="comment">/* 把 str1 所指向的字符串和 str2 所指向的字符串进行比较。 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">strcmp</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *str1, <span class="type">const</span> <span class="type">char</span> *str2)</span> </span><br></pre></td></tr></table></figure>




    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/582937ae.html">LV01-05-C语言-数组与字符串</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年01月14日 - 16:29</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/582937ae.html" title="LV01-05-C语言-数组与字符串">https://sumumm.github.io/post/582937ae.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV01-C语言基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/84847102.html" rel="prev" title="LV01-06-C语言-指针">
                  <i class="fa fa-angle-left"></i> LV01-06-C语言-指针
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/d7b81674.html" rel="next" title="LV01-04-C语言-控制语句">
                  LV01-04-C语言-控制语句 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
